/**
 * @file       NatOrRouterModeSettingsPanel.cpp
 * @brief      TODO
 * @author     Huabo Zheng
 * @date       Feb 3, 2012
 * @Copyright  2012, www.netgear.com Inc. All rights reserved.
 */

#include "NatOrRouterModeSettingsPanel.h"
#include "errorcode.h"
#include "MenuButton.h"
#include "IEventListener.h"
#include "typedefine.h"
#include "KeyboardListener.h"
#include <sstream>
#include "Rect.h"
#include "EventHandler.h"
#include "ZoneSettingPanel.h"
#include <cstdio>

using namespace  NS_LCD_AFW;


CNatOrRouterModeSettingsPanel::CNatOrRouterModeSettingsPanel()
{
}

CNatOrRouterModeSettingsPanel::~CNatOrRouterModeSettingsPanel()
{
}

int CNatOrRouterModeSettingsPanel::onCreate()
{
	//TODO:get the real Zone number here
	int zones = 205;//sysGetZoneNumber(); TODO:get the real Zone number here
	int iRet = ERR_SUCCESS;
	CMenuButton *pMenuItem = NULL;
	IEventListener *pListener = NULL;

	for (int i = 0; i < zones; ++i)
	{
		std::ostringstream os;
		os << "Zone" << i + 1;
		OBJ_NEW(CMenuButton, pMenuItem);
		if (NULL == pMenuItem || ERR_SUCCESS != (iRet = pMenuItem->create(
				os.str(), CRect(1,i + 1,LCD_WIDTH,1), this)))
		{
			goto err_proc;
		}
		pListener = new CKeyboardListener(this,
				ENTER_KEY_PRESSED_EVENT_ID,
				(AFW_EventCallBack)(&CNatOrRouterModeSettingsPanel::onZoneXClick),
				i + 1 ,
				true);
		if (NULL == pListener || ERR_SUCCESS != (iRet
				= pMenuItem->addEventListener(pListener)))
		{
			goto err_proc;
		}
		pMenuItem->setText(os.str());
		if (ERR_SUCCESS != (iRet = this->add(pMenuItem)))
		{
			goto err_proc;
		}
	}
	return CPanel::onCreate();

	err_proc: if (NULL != pListener)
	{
		pListener->destory();
	}
	if (NULL != pMenuItem)
	{
		pMenuItem->destory();
		pMenuItem = NULL;
	}
	return iRet;

}

int CNatOrRouterModeSettingsPanel::onEnterKeyPress()
{
	// Change to Nat configuration items screen
	return CPanel::onEnterKeyPress();
}

int CNatOrRouterModeSettingsPanel::onEscKeyPress()
{
	// Return To Main Menu Panel

	return CPanel::onEscKeyPress();
}

int CNatOrRouterModeSettingsPanel::onUpKeyPress()
{
	//Switch to prev Menu Item
	return CPanel::onUpKeyPress();
}

int CNatOrRouterModeSettingsPanel::onDownKeyPress()
{
	// Switch to next Menu Item
	return CPanel::onDownKeyPress();
}

void CNatOrRouterModeSettingsPanel::onZoneXClick(int eventId,
		int param)
{
	int zoneNo = param;
	//printf("zone(%d) show\n",zoneNo);
	//TODO: read the config of the zone
	//	switch (zoneNo)
	//	{
	//	case ZONE1:
	//		readZone1Cfg();
	//		break;
	//	case ZONE2:
	//		readZone2Cfg();
	//		break;
	//		...
	//	default:
	//		break;
	//	}


	int iRet = ERR_SUCCESS;
	CZoneSettingPanel *pPanel = NULL;

	OBJ_NEW(CZoneSettingPanel, pPanel);
	if (NULL == pPanel
			|| ERR_SUCCESS
					!= (iRet = pPanel->create("CZoneSettingPanel",
							DEFAULT_RECT, this)))
	{
		OBJ_DELETE(pPanel);
		return;
	}

	pPanel->setIpAddress("192.168.0.99");
	pPanel->setNetMask("255.255.255.0");
	pPanel->setGateway("192.168.0.1");
	//pPanel->setZoneNo(zoneNo);
	pPanel->show();
}

